package com.bgs.controller;


import com.alibaba.fastjson.JSONObject;
import com.bgs.common.R;
import com.bgs.pojo.ReturnGoods;
import com.bgs.pojo.vo.OrderTow;
import com.bgs.service.ReturnGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 退货表 前端控制器
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/bgs/return-goods")
public class ReturnGoodsController {
    @Autowired
    private ReturnGoodsService returnGoodsService;
    @GetMapping("/TowGetReturn")
    public R TowGetReturn(Integer beforeUserId){
        List<ReturnGoods> list = returnGoodsService.TowGetReturn(beforeUserId);
        return R.ok().data("list",list);
    }

    /**
     * {
     * orderId:1,
     * orderDetailId:1,
     * returnGoodsReason:'商品残缺'.
     * beforeUserId:1,
     * returnGoodsPayment:10,
     * returnGoodsPaymentType:1,
     * }
     */

    @PostMapping("zeroAddReturnGood")
    public R zeroAddReturnGood(@RequestBody ReturnGoods returnGoods) {
        boolean b = returnGoodsService.zeroAddReturnGood(returnGoods);
        return R.ok().data("b", b);
    }

    //查询提交的售后
    @GetMapping("zeroFindReturnGood3")
    public R zeroFindReturnGood3() {
        return returnGoodsService.zeroFindReturnGood3();
    }

    //后台审核后，修改为允许退货0
    @GetMapping("zeroUpdateReturnGood0")
    public R zeroUpdateReturnGood0(Integer returnGoodsId) {
        boolean b = returnGoodsService.zeroUpdateReturnGood0(returnGoodsId);
        return R.ok().data("b", b);
    }

    //添加快递号
    @PostMapping("zeroAddshipping")
    public R zeroAddshipping(@RequestBody ReturnGoods returnGoods) {
        boolean b = returnGoodsService.zeroAddshipping(returnGoods);
        return R.ok().data("b", b);
    }

    //店家收货后，
    // 存入mq，店家减钱，用户加钱
    // 修改为交易完成1
    @PostMapping("zeroUpdateReturnGood1")
    public R zeroUpdateReturnGood1(@RequestBody ReturnGoods returnGood) {
        boolean b = returnGoodsService.zeroUpdateReturnGood1(returnGood);
        return R.ok().data("b", b);
    }
    //退款成功后，Order表状态改为4
//    @GetMapping("zeroUpdateOrder4")
//    public R zeroUpdateOrder4(Integer orderId) {
//        return R.ok().data("b",b);
//    }

    @PostMapping("/TowReturnAdd")
    public R TowReturnAdd(@RequestBody Map map){
        String data1 = JSONObject.toJSONString(map.get("data"));
        OrderTow orderTow = JSONObject.parseObject(data1, OrderTow.class);
        String returnGoodsReason = map.get("returnGoodsReason").toString();
        ReturnGoods returnGoods = new ReturnGoods();
        returnGoods.setOrderId(orderTow.getOrderId());
        returnGoods.setOrderDetailId(orderTow.getOrderMiddleDetail().getOrderDetailId());
        returnGoods.setReturnGoodsReason(returnGoodsReason);
        returnGoods.setReturnGoodsPaymentType(3);
        returnGoods.setReturnGoodsStatus(3);
        returnGoods.setCreateTime(new Date());
        returnGoods.setShippingCompName2("顺丰");
        returnGoods.setShippingSerialNumber(Math.random()*100000+1+"");
        returnGoodsService.TowReturnAdd(returnGoods);
        return R.ok();
    }


}